package com.alibaba.citrus.cr.mn.order.center.facade.function.asynctask.consumer;

import com.alibaba.citrus.cr.mn.order.center.facade.function.asynctask.AsyncTaskCreateService;
import com.alibaba.citrus.cr.mn.order.center.facade.function.asynctask.OrderAsyncTaskListenerConsumerSelector;
import com.alibaba.citrus.cr.mn.order.center.facade.function.common.OrderBizCreateService;
import com.alibaba.citrus.cr.mn.order.center.facade.function.common.SendMsgService;
import com.alibaba.citrus.cr.mn.order.center.facade.repository.AtomOrderQueryRepository;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.crb2btradecenter.messagetask.model.MessageTask;
import com.epoch.app.crb2btradecenter.model.enums.MessageTaskTypeEnum;
import com.epoch.app.mnordercenter.model.dto.CommonAsyncMessageResponse;
import com.epoch.app.mnordercenter.model.dto.ReturnOrderAtomBatchCreateRequest;
import com.epoch.app.mnordercenter.model.dto.ReturnOrderBizBatchCreateRequest;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomCreateResponse;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhangxiaorui
 * @ClassName OrderAsyncTaskSalesOrderCreateService
 * @description: TODO
 * @date 2023/06/06 11:12
 * @version: 1.0
 */
@Component("orderAsyncTaskReturnOrderCreateComponent")
public class OrderAsyncTaskReturnOrderCreateComponent implements OrderAsyncTaskListenerConsumerSelector {
    private static final Log log = Log.getLogger(OrderAsyncTaskReturnOrderCreateComponent.class);

    @Resource
    private SendMsgService sendMsgService;

    @Resource
    private AsyncTaskCreateService asyncTaskCreateService;

    @Resource
    private AtomOrderQueryRepository atomOrderQueryRepository;

    @Resource(name = "returnOrderBizCreateService")
    private OrderBizCreateService<ReturnOrderBizBatchCreateRequest, ReturnOrderAtomBatchCreateRequest, OrderAtomCreateResponse> returnOrderBizCreateService;

    @Override
    public MessageTaskTypeEnum getMessageTaskType() {
        return MessageTaskTypeEnum.OC_RETURN_ORDER_BATCH_CREATE;
    }

    @Override
    public void doConsumer(String ossTaskContent, MessageTask messageTask) {
        // 商业服务调用
        ReturnOrderBizBatchCreateRequest createRequest = JSONObject.parseObject(ossTaskContent, ReturnOrderBizBatchCreateRequest.class);
        List<OrderAtomCreateResponse> createResponses = returnOrderBizCreateService.batchCreate(createRequest);

        log.info("OrderAsyncTask_doCreate_ReturnOrder result={}", JSONArray.toJSONString(createResponses));

        // 发送kafka消息通知应用端处理结果
//        sendMsgService.sendMsgToNYK(convertMnSideRequests(createResponses));
        CommonAsyncMessageResponse commonAsyncMessageResponse = convertCommonAsyncMessageResponse(messageTask.getBatchNo(), createRequest.getFromSys(), createRequest.getBusinessUnitCode(), createResponses);
        sendMsgService.sendMsgToSendKafka(commonAsyncMessageResponse);
        log.info("OrderAsyncTaskReturnOrderCreateComponent#doConsumer sendMsgToSendKafka message={}", commonAsyncMessageResponse);

        // 上传原子结果
        asyncTaskCreateService.appendAsyncTaskHandleResult(JSONArray.toJSONString(createResponses),
                messageTask.getBatchNo(), messageTask);
    }

    @Override
    public void sendOccupyInventoryOccupyMsg(List<String> bizOrderId) {

    }
}
